package cn.wd.user.controller;


import cn.wd.common.vo.R;
import cn.wd.user.domain.entity.Department;
import cn.wd.user.page.PageResult;
import cn.wd.user.qo.DepartmentQueryObject;
import cn.wd.user.service.IDepartmentService;
import cn.wd.user.vo.BatchEmp;
import cn.wd.user.vo.JsonResult;
import cn.wd.user.vo.TreeData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    
    private final IDepartmentService departmentService;

    public DepartmentController(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @RequestMapping("/list")
    public R<Page<Department>> list(DepartmentQueryObject qo) {
        return R.OK(departmentService.listDepartmentPage(qo));
    }

    @RequestMapping("/all")
    public R<List<Department>> all() {
        return R.OK(departmentService.list());
    }

    @RequestMapping("/treeList")
    public JsonResult<List<TreeData>> treeList() {
        List<TreeData> treeList = departmentService.queryTreeList();
        return JsonResult.success(treeList);
    }

    @RequestMapping("/saveOrUpdate")
    public JsonResult<?> saveOrUpdate(@RequestBody Department department) {
        if (department.getId() != null) {
            departmentService.updateById(department);
        } else {
            departmentService.save(department);
        }
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    public R<?> delete(Long id) {
        if (id == null) {
            return R.FAIL();
        }
        departmentService.removeById(id);
        return R.OK();
    }

    // TODO: 根据部门统计对应的员工人数
    @RequestMapping("/batchemp")
    public JsonResult<List<BatchEmp>> queryDepartment() {
        List<BatchEmp> queryDept = departmentService.batchEmp();
        return JsonResult.success(queryDept);
    }
}
